<?php
// +----------------------------------------------------------------------
// | Powered By ThinkPHP 5.0.12
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.bojuwang.net All rights reserved.
// +----------------------------------------------------------------------
// | Date：2018-1
// +----------------------------------------------------------------------
// | Author: ZhengLuwei <512711380@qq.com>
// +----------------------------------------------------------------------
namespace app\bjwlmanager\controller;
use think\Db;
class Article extends ManagerBasic
{
    /**
     * 加载用户信息列表
     */
    public function index()
    {
        $categoryId = input('param.category_id');
        $keywords = input('param.keywords');
        $where=[];
        $categoryId ? $where['category_id']=$categoryId :'';
        $keywords ? $where['title'] = ['like',"%{$keywords}%"]:'';
        $list = Db::name('article')->order('id desc')->where($where)->paginate(15,false,['query'=>request()->param()])->each(function($item, $key){
            $model=get_category_model($item['category_id']);
            if($model)
            {
                $item['categoryshow']=$model['title'];
            }else{
                $item['categoryshow']="未设置";
            }
            return $item;
        });
        $this->assign('articleList',$list);
        unset($list);
        $category = Db::name('sys_category')->where('channel_id',1)->select();
        $this->assign('category',$category);
        return view();
    }
    /**
     * 添加会员
     */
    public function add()
    {
        $categoryList=Db::name('sys_category')->where('channel_id',1)->order('id desc')->select();
        $this->assign('categoryList',$categoryList);
        unset($categoryList);
        return view();
    }
    public function edit()
    {
        $id=input('get.id');
        $articleInfo=Db::name('article')->where('id',$id)->find();
        $categoryList=Db::name('sys_category')->where('channel_id',1)->order('id desc')->select();
        $this->assign('categoryList',$categoryList);
        unset($categoryList);
        $this->assign('articleInfo',$articleInfo);
        return view();
    }
    /**
     * 保存文章
     */
    public function addsave()
    {
        $title=input('post.title');
        $author=input('post.author');
        $categoryId=input('post.category_id');
        $content=input('post.content');
        $summary=input('post.summary');
        $sort=input('post.sort');
        $img_url =input('post.img_url');
        
        $info=array();
        $info['title']=$title;
        $info['author']=$author;
        $info['img_url']= $img_url;
        $info['content']=$content;
        $info['summary']=$summary;
        $info['sort']=$sort;
        $info['status']=0;
        $info['channel_id']=1;
        $info['category_id']=$categoryId;
        
        $result=Db::name('article')->insert($info);
        unset($info);
        if($result==0)
        {
           json_msg(1,'添加失败');
        }
        else{
           json_msg(0,'添加成功');
        }
    }
    /**
     * 编辑保存
     */
    public function editsave()
    {
        $id=input('post.id');
        $title=input('post.title');
        $author=input('post.author');
        $categoryId=input('post.category_id');
        $content=input('post.content');
        $summary=input('post.summary');
        $sort=input('post.sort');
        $img_url = input('post.img_url');
        
        $info=array();
        $info['title']=$title;
        $info['author']=$author;
        $info['img_url'] = $img_url;
        $info['category_id']=$categoryId;
        $info['content']=$content;
        $info['summary']=$summary;
        $info['sort']=$sort;
        
        $result=Db::name('article')->where('id',$id)->update($info);
        unset($info);
        if($result==0)
        {
            json_msg(1,'更新失败');
        }
        else{
            json_msg(0,'更新成功');
        }
        
    }
    //删除指定文章
    public function del()
    {
        $articleId=input('post.articleId');
        if(!isset($articleId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('article')->where('id',$articleId)->delete();
        if($result==0)
        {
            json_msg(1,'删除失败');
        }
        else
        {
            json_msg(0,'删除成功');
        }
    }
    //停用
    public function stop()
    {
        $articleId=input('post.articleId');
        if(!isset($articleId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('article')->where('id',$articleId)->setField('status',1);
        if($result==0)
        {
            json_msg(0,'状态未更改');
        }
        else
        {
            json_msg(0,'已更改为未审核');
        }
    }
    //启用
    public function start()
    {
        $articleId=input('post.articleId');
        if(!isset($articleId))
        {
            json_msg(1,'请求参数错误');
        }
        $result=Db::name('article')->where('id',$articleId)->setField('status',0);
        if($result==0)
        {
            json_msg(0,'状态未更改');
        }
        else
        {
            json_msg(0,'审核通过');
        }
    }
}